package movie.util;

import com.google.common.base.Joiner;
import org.apache.hadoop.conf.Configuration;
import org.apache.sqoop.Sqoop;
import org.apache.sqoop.hive.HiveConfig;
import org.apache.sqoop.tool.ExportTool;
import org.apache.sqoop.tool.SqoopTool;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class SqoopUtil {

    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static void export(String table, String dir,String[] col) throws IOException {
        LocalDate date  = LocalDate.now();
        String time = formatter.format(date);
        String cols = Joiner.on(",").join(col);
        String[] argument = new String[] {
                "--connect","jdbc:mysql://localhost:3306/movie_bigdata?default-character-set=utf8&serverTimezone=UTC",
                "--username","root",
                "--password","root",
                "--table",table,
                "--columns",cols,
                "--export-dir","/data/inner/FCT/"+time+"/" + dir,
        };
        ExportTool sqoopTool = new ExportTool();
        Configuration conf= new Configuration();
        conf.set("fs.defaultFS","hdfs://master:9000");
        Configuration hive= HiveConfig.getHiveConf(conf);
        Sqoop sqoop = new Sqoop(sqoopTool,SqoopTool.loadPlugins(conf) );
        int res = Sqoop.runSqoop(sqoop,argument);
        System.out.println(res);
    }
}